// package/pages/setting/setting.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');

let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: '',
    name: '',
    mobile: ''
  },
  //上传头像
  onChooseAvatar(e) {
    const that = this;
    wx.getImageInfo({
      src: e.detail.avatarUrl,
      success: function (result) { //访问存放微信用户头像的Url 
        wx.showLoading({
          title: '加载中...',
        })
        wx.uploadFile({
          filePath: result.path,
          name: 'head_pic',
          url: api.reqUrls.uploadHeadPic,
          method: "post",
          header: {
            "Content-Type": "multipart/form-data",

          },
          success(porp) {
            wx.hideLoading();
            let imgUrl = JSON.parse(porp.data)
            let data = {
              head_pic: imgUrl.result
            }
            myRequest.EasyHttp.post(api.reqUrls.setHeadPic, data).then(res => {
              wx.hideLoading();
              if (res.data.status == 1) {
                that.setData({
                  avatarUrl: imgUrl.result
                })
                wx.showToast({
                  title: '设置成功',
                  icon: 'success',
                  duration: 2000
                })
                let pages = getCurrentPages(); //获取小程序页面栈
                let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
                beforePage.getData(); //触发上个页面自定义的shuaxin方法
              } else {
                wx.showModal({
                  showCancel: false,
                  content: res.data.message,
                  success(res) {
                    if (res.confirm) {

                    }
                  }
                })
              }
            })
          },
          error(err) {
            console.log(err, 555)
          }
        })
      },
      fail(err) {
        console.log(err, 666)
      }
    })

  },
  // 设置用户名
  inpName(e) {
    if (e.detail.value) {
      let data = {
        nickname: e.detail.value
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.setNickname, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.showToast({
            title: '设置成功',
            icon: 'success',
            duration: 2000
          })
          let pages = getCurrentPages(); //获取小程序页面栈
          let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
          beforePage.getData(); //触发上个页面自定义的shuaxin方法
        } else {
          wx.showToast({
            title: '设置失败',
            icon: 'error',
            duration: 2000
          })
        }
      })
    } else {
      wx.showToast({
        title: '请输入用户名',
        icon: 'error',
        duration: 2000
      })
    }
  },
  // 获取用户信息
  getData() {
    const that = this;
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.userInfo).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          avatarUrl: res.data.result.head_pic,
          name: res.data.result.nickname,
          mobile: res.data.result.mobile,
        })

      }
    })
  },
   //绑定手机号
   getPhoneNumber(e) {
    const that = this;
    console.log(e.detail.code)
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      let data = {
        code: e.detail.code,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.bindMiniPhone, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          })
         that.setData({
          mobile:res.data.phoneinfo.mobile
         })
        } else {
          wx.showModal({
            showCancel: false,
            content: res.data.message,
            success(res) {
              if (res.confirm) {

              }
            }
          })
        }
      })
    } else {
      if (e.detail.errno == '104') {
        wx.showToast({
          title: '10秒后可以点击',
          icon: 'error',
          duration: 2000
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})